#include "darkheap.h"

DarkHeap::DarkHeap()
{
    this->len=0;
    this->n=1;
}

void DarkHeap::resize(int sz){
    this->len=sz+1;
    this->hp.resize(this->len);
}
void DarkHeap::push(DarkNode nd){
    if (n<this->len){
        hp[n]=nd;
        swim(n);
        n++;
    }else{
        if (nd.val<hp[n-1].val){
            hp[n-1]=nd;
            swim(n-1);
        }
    }
}

void DarkHeap::swim(int pos){
    int nx = pos>>1;
    DarkNode aim = hp[pos];
    while (nx>0 && aim.val < hp[nx].val ){
        hp[pos]=hp[nx];
        pos=nx;
        nx=pos>>1;
    }
    hp[pos]=aim;
}

void DarkHeap::show(){
    qDebug()<<"tot elem: "<<n;
    int i;
    for (i=1;i<n;i++){
        hp[i].show();
    }
}

